package net.Stratus.ImageDumper.GUI;

import java.util.ArrayList;

import net.Stratus.ImageDumper.Logger;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Text;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.graphics.Point;

public class JobViewer {
	
	private static ArrayList<JobViewer> instances = new ArrayList<JobViewer>();
	private int jobid;
	protected Shell shell;
	private Text textLogger;

	/**
	 * Creates a new JobView Window
	 * 
	 * @param the id of the job to view
	 * @wbp.parser.entryPoint
	 */
	public static void create(int jobid) {
		// only one window per job
		for(JobViewer jv : instances) {
			if(jv.jobid == jobid) {
				jv.bringToFront();
				return;
			}
		}
		try {
			JobViewer window = new JobViewer(jobid);
			window.open();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	/**
	 * Creates a new job viewer
	 * 
	 * @param jobid id of job
	 */
	public JobViewer(int jobid) {
		this.jobid = jobid;
	}
	
	
	/**
	 * Checks if there are open windows left
	 * @return if there are open windows left
	 */
	public static boolean hasWindows() {
		return (instances.size() > 0);
	}
	
	/**
	 * Brings this window to the front... hopefully
	 */
	private void bringToFront() {
	    shell.getDisplay().asyncExec(new Runnable() {
	        public void run() {
	            shell.forceActive();
	        }
	    });
	}

	
	/**
	 * Open the window.
	 */
	public void open() {
		Display display = Display.getDefault();
		createContents();
		shell.open();
		shell.layout();
		instances.add(this);
		Integer iJobID = new Integer(jobid);
		Logger.newWindow(iJobID);
		while (!shell.isDisposed()) {
			if (!display.readAndDispatch()) {
				display.sleep();
			}
		}
		Logger.removeWindow(iJobID);
		instances.remove(this);
	}

	/**
	 * Create contents of the window.
	 */
	protected void createContents() {
		shell = new Shell(SWT.SHELL_TRIM);
		shell.setMinimumSize(new Point(131, 9));
		shell.setSize(570, 287);
		shell.setText("Viewing Job "+jobid);
		shell.setLayout(new GridLayout(1, false));
		
		Composite composite = new Composite(shell, SWT.NONE);
		composite.setLayout(new FillLayout(SWT.HORIZONTAL));
		composite.setLayoutData(new GridData(SWT.FILL, SWT.TOP, false, false, 1, 1));
		
		Button btnResume = new Button(composite, SWT.NONE);
		btnResume.setEnabled(false);
		btnResume.setText("Resume");
		
		Button btnPause = new Button(composite, SWT.NONE);
		btnPause.setText("Pause");
		
		Button btnStop = new Button(composite, SWT.NONE);
		btnStop.setText("Stop");
		
		textLogger = new Text(shell, SWT.BORDER | SWT.READ_ONLY | SWT.H_SCROLL | SWT.V_SCROLL | SWT.CANCEL);
		textLogger.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true, 1, 1));
		textLogger.setText(Logger.getLogFile(jobid));

	}


	/**
	 * Calls updateDate asynchronously on all windows
	 */
	public static void updateDataAll() {
		// loop through all instances and update them
		for (int i = 0; i < instances.size(); i++) {
			// by calling updateDate asynchronously
			final JobViewer jv = instances.get(i);
			Display.getDefault().syncExec(new Runnable() {
				public void run() {
					jv.updateData();
		        }
			});
		}
	}
	
	/**
	 * Update text in textlogger
	 */
	public void updateData() {
		if(textLogger.isDisposed()) {
			instances.remove(this);
			return;
		}
		// append any new text
		textLogger.setRedraw(false);
		int scrollP = textLogger.getTopIndex();
		Point selectionP = textLogger.getSelection();              
		textLogger.append(Logger.getLog(jobid));
		textLogger.setSelection(selectionP);
		textLogger.setTopIndex(scrollP);
		textLogger.setRedraw(true);
	}

}
